import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_res_team_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/resource/detail/model.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/resource/team/team_ctrl.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 应急队伍
class EmergencyResourceTeamPage extends StatelessWidget {
  EmergencyResourceTeamPage({super.key});

  final controller = Get.put(EmergencyResourceTeamController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          key: controller.popRightScaffoldKey,
          endDrawer: Container(
            width: AppDimens.sw - 80.w,
            height: AppDimens.sh,
            color: Colors.white,
            child: _getFilterWidget(),
          ),
          body: Container(
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: [
                _buildFilter(),
                AppGaps.vGap10,
                _buildMain(),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildFilter() {
    return Row(
      children: [
        Expanded(
          child: CustomSearchEnter(
            height: 32.w,
            hintText: '请输入名称',
            text: controller.teamName,
            onCallBack: (value) {
              controller.teamName = value;
              controller.update();
              controller.refreshPage();
            },
          ),
        ),
        AppGaps.hGap8,
        Container(
          width: 36.w,
          height: 32.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
          child: SvgPicture.asset(
            AppImages.icon_search_screen,
            width: 18.w,
            height: 16.w,
          ),
        ).clickOnce(controller.screen),
      ],
    );
  }

  Widget _buildMain() {
    return Expanded(
      child: CustomEasyRefreshList<EmergencyResTeamEntity>(
        key: controller.globalKey,
        dataLoad: (pageIndex, pageSize, {String? search}) async {
          return controller.getListData(pageIndex, pageSize);
        },
        itemBuilder: (item, index) {
          return CustomTitleChartPage(
            title: item.teamName,
            titleRightWidget: _buildCardTopRight(item.estimateName),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CustomTextWidget.getTextRowView(title: '队伍分类：', value: item.teamClassificationName ?? '未知'),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '总人数：', value: item.totalNumberOfPeople?.toString() ?? '未知'),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '负责人：', value: item.responsiblePerson ?? '未知'),
              ],
            ),
          ).clickOnce(() {
            Get.toNamed(
              AppRoutes.emergencyResourceDetails,
              arguments: {
                'type': EmergencyResourceDetailPageType.team,
                'data': item,
              },
            );
          });
        },
      ),
    );
  }

  /// 卡片右上角
  Widget _buildCardTopRight(String? state) {
    late final Color color;
    switch (state) {
      case '已评估':
        color = Colors.green;
        break;
      default:
        color = Colors.red;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        state ?? '未评估',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            //渐变颜色[始点颜色, 结束颜色]
            colors: [
              Color(0xffE7EAFF),
              Colors.white,
              Colors.white,
              Colors.white,
            ]),
        // borderRadius: BorderRadius.only(
        //     topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w)),
        // color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppGaps.vGap30,
          Text(
            '   筛选',
            style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w500,
              color: Colors.black,
            ),
          ),
          AppGaps.vGap20,
          Expanded(
            child: ListView(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              children: [
                const Text('敏感目标名称'),
                AppGaps.vGap20,
                CustomSearchEnter(
                  showIcon: false,
                  height: 32.w,
                  hintText: '请输入名称',
                  text: controller.teamName,
                  onCallBack: (value) {
                    controller.teamName = value;
                    controller.update();
                  },
                ),
                AppGaps.vGap20,
                const Text('队伍分类'),
                AppGaps.vGap20,
                CustomDropdownSelection(
                  hintText: '请选择队伍分类',
                  text: controller.teamT?.name,
                  onCallBack: controller.showSelectTeamType,
                ),
                AppGaps.vGap20,
                const Text('所属单位'),
                AppGaps.vGap20,
                CustomSearchEnter(
                  showIcon: false,
                  height: 32.w,
                  hintText: '请输入所属单位',
                  text: controller.teamName,
                  onCallBack: (value) {
                    controller.teamName = value;
                    controller.update();
                  },
                ),
                AppGaps.vGap20,
                const Text('评估状态'),
                AppGaps.vGap20,
                CustomDropdownSelection(
                  hintText: '请选择评估状态',
                  text: controller.assess?.name,
                  onCallBack: controller.showSelect,
                ),
              ],
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Container(
                  height: 40.w,
                  width: 100.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: const Color(0xffffffff),
                      border: Border.all(color: const Color(0xff5C6DFF), width: 1.w),
                      borderRadius: BorderRadius.all(Radius.circular(20.w))),
                  child: Text(
                    '重置',
                    style: TextStyle(
                      fontSize: 18.sp,
                      color: const Color(0xff5C6DFF),
                    ),
                  )).clickOnce(controller.clearChecked),
              Container(
                  height: 40.w,
                  width: 100.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(color: const Color(0xff5C6DFF), borderRadius: BorderRadius.all(Radius.circular(20.w))),
                  child: Text(
                    '确定',
                    style: TextStyle(
                      fontSize: 18.sp,
                      color: Colors.white,
                    ),
                  )).clickOnce(controller.confirm),
            ],
          ),
          AppGaps.vGap20,
        ],
      ),
    );
  }
}
